//
//  ViewControllerForPhotoList.m
//  世界
//
//  Created by admin on 16/2/17.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "ViewControllerForPhotoList.h"
#import <UIImageView+WebCache.h>
#import <MJRefresh/MJRefresh.h>
#import "ModelForAll.h"
#import "CellForPhotoList.h"
#import "ViewControllerForPhotoDetail.h"
#import "NetworkingHandler.h"

@interface ViewControllerForPhotoList ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, retain) UITableView *tabView;
@property (nonatomic, retain) NSMutableArray *arrUrl;
@property (nonatomic, retain) NSMutableArray *arrId;
@property (nonatomic, retain) NSMutableArray *arrTitle;
@property (nonatomic, assign) NSInteger page;

@end

@implementation ViewControllerForPhotoList

#pragma mark 重新方法 dealloc
- (void)dealloc
{
    [_tabView release];
    
    [_arrUrl release];
    
    [_arrId release];
    
    [_arrTitle release];
    
    [super dealloc];
}



#pragma mark 视图的生命周期 <<<<<<
#pragma mark 视图加载完成
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.page = 1;
    
    // self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    
    self.navigationItem.title = @"美图";
    
    //处理数据
    [self handleData];
    
    //添加tabview
    [self createTableview];
    
    self.arrUrl = [NSMutableArray array];
    
    self.arrId = [NSMutableArray array];
    
    self.arrTitle = [NSMutableArray array];
    
    
    self.tabView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        
        [self.tabView reloadData];
        
        [self.tabView.mj_header endRefreshing];
        
    }];
    
    self.tabView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        // 进入刷新状态后会自动调用这个block
        [self handleData];
        
    }];
}



#pragma mark 收到内存警告!!!
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}



#pragma mark 自定义方法 <<<<<<
#pragma mark 添加数据
- (void)handleData
{
    //设置访问地址
    
    NSString *url = [NSString stringWithFormat:@"http://dili.bdatu.com/jiekou/mains/p%ld.html", self.page++];
    
    
    
    [NetworkingHandler netWorkingWithURL:url completeHandle:^(NSURLResponse *response, NSData *data ,id result) {
        
        ModelForAll *model = [[ModelForAll alloc] init];
        
        [model setValuesForKeysWithDictionary:result];
        
        [model release];
        
        for (NSDictionary *dic in model.album)
        {
            NSString *str1 = [dic objectForKey:@"url"];
            
            NSString *str2 = [dic objectForKey:@"id"];
            
            NSString *str3 = [dic objectForKey:@"title"];
            
            [self.arrUrl addObject:str1];
            
            [self.arrId addObject:str2];
            
            [self.arrTitle addObject:str3];
        }
            
            [self.tabView reloadData];
            
            [self.tabView.mj_footer endRefreshing];
        
    }];

}

#pragma mark 添加tabview
- (void)createTableview
{
    self.tabView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    [self.view addSubview:self.tabView];
    
    self.tabView.dataSource = self;
    
    self.tabView.delegate = self;
    
    [self.tabView registerClass:[CellForPhotoList class] forCellReuseIdentifier:@"pool1"];
    
    //去掉分割线
    self.tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
}



#pragma mark UITableViewDataSource 协议方法
#pragma mark 相应分区的行的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrUrl.count;
}



#pragma mark 给每行添加cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = [self.arrUrl objectAtIndex:indexPath.row];
    
    CellForPhotoList *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
    
    //去掉选择cell之后背景颜色灰的效果
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.lableForTitle.text = [self.arrTitle objectAtIndex:indexPath.row];
    
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"占位图"]];
    
    return cell;
}



#pragma mark UITableViewDelegate 协议方法
#pragma mark 设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 300;
}


#pragma mark 选择相应的行所触发的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ViewControllerForPhotoDetail *photoDetail = [[ViewControllerForPhotoDetail alloc] init];
    
    photoDetail.id = [self.arrId objectAtIndex:indexPath.row];
    
    //1.设置self.tabBarController.tabBar.hidden=YES;
    self.tabBarController.tabBar.hidden = YES;
    
    //2.如果在push跳转时需要隐藏tabBar，设置self.hidesBottomBarWhenPushed=YES;
    self.hidesBottomBarWhenPushed=YES;
    
    [self.navigationController pushViewController:photoDetail animated:YES];
    
    
    //3.在push后设置self.hidesBottomBarWhenPushed=NO,这样back回来的时候，tabBar会恢复正常显示。
    self.hidesBottomBarWhenPushed=NO;
}


@end
